package com.whales.lms.rms.web.service;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import anyframe.common.Page;

import com.whales.lms.rms.web.model.FaultVO;
import com.whales.lms.rms.web.model.PcVO;
import com.whales.lms.rms.web.util.ConstantUtil;
import com.whales.shared.service.GenericService;

@Service
public class FaultService extends GenericService<FaultVO, FaultVO> {
	@Inject
	private PcService pcService;
	
	@SuppressWarnings("unchecked")
	public List<Map<String,String>> getDayilyCount() throws Exception{
		return super.commonDao.findList("fault.geDayilyCount", "");
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,String>> getWeeklyCount() throws Exception{
		return super.commonDao.findList("fault.geWeeklyCount", "");
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,String>> getMonthlyCount() throws Exception{
		return super.commonDao.findList("fault.geMonthlyCount", "");
	}
	
	@Override
	public List<FaultVO> getList(FaultVO vo) throws Exception {
		return super.getList("fault.getFaultList",vo);
	}
	
	@Override
	public Page getPageList(FaultVO vo) throws Exception {
		return super.getPageList("fault.getFaultList", vo, vo.getStart(), vo.getLimit());
	}
	
	@Override
	public int create(FaultVO vo) throws Exception {
		//PC 상태 update
		PcVO pcVO = pcService.findByIp(vo.getIp());
		pcVO.setIp(vo.getIp());
		if(ConstantUtil.TYPE_SW.equals(vo.getSepType())){
			pcVO.setSwState(vo.getFaultCode());
			pcVO.setHwState(null);
		}else{
			pcVO.setHwState(vo.getFaultCode());
			pcVO.setSwState(null);
		}
		if(pcService.updateStateByIp(pcVO) > 0){ //업데이트 건수가 0보다 큰 경우는 기존 상태값이 틀린 경우이므로 fault_list에서 insert 한다.
			//Fault 생성
			vo.setParentId(pcVO.getId());
			//상태가 ??? -> OKE로 변경인 경우 복구일자를 업데이트 한다.
			if(ConstantUtil.STATE_OKE.equalsIgnoreCase(vo.getFaultCode())){
				this.updateRecoverDate(vo);
			}
			return super.create("fault.createFault",vo);
		}else{
			return super.create(vo);
		}
	}
	
	private int updateRecoverDate(FaultVO vo) throws Exception{
		return super.update("fault.updateRevocerDdate", vo);
	}
}
